﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using Microsoft.Reporting.WinForms;

namespace Billing.Win
{
    public partial class ClientStatementsForm : Billing.WindowsBase.FormBase
    {
        public ClientStatementsForm()
        {
            InitializeComponent();
            this.dtFrom.Value = DateTime.Now.AddMonths(-1);
            this.dtTo.Value = DateTime.Now;
        }

        BillingDataContext mBillingDataContext;
        public BillingDataContext BillingDataContext
        {
            get
            {
                if (mBillingDataContext == null)
                    mBillingDataContext = new BillingDataContext();
                return mBillingDataContext;
            }
            set { mBillingDataContext = value; }
        }

        private int mClientID;

        public int ClientID
        {
            get { return mClientID; }
            set { mClientID = value; }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void onSearch()
        {
            //this.bsStatements.DataSource = this.BillingDataContext.StatementSelectByPeriod(
            //                                                        this.ClientID,
            //                                                        null,
            //                                                        null,
            //                                                        (DateTime)this.dtFrom.Value,
            //                                                        (DateTime)this.dtTo.Value);
            //this.numBegBalance.Value = this.BillingDataContext.GetClientBalanceForClientInfo(this.ClientID, this.dtFrom.DateTime.AddDays(-1)).Value;
            LoadReport();
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.onSearch();
        }

        private void ClientStatementsForm_Load(object sender, EventArgs e)
        {

            this.onSearch();
            this.reportViewer1.RefreshReport();
        }

        private void cmbMonthFrom_ValueChanged(object sender, EventArgs e)
        {
            if (tag == true)
                this.dtFrom.DateTime = new DateTime(this.dtFrom.DateTime.Year, (int)this.cmbMonthFrom.Value, 1);
        }

        private void cmbMonthTo_ValueChanged(object sender, EventArgs e)
        {
            if (tag == true)
                this.dtTo.DateTime = new DateTime(this.dtTo.DateTime.Year, (int)this.cmbMonthTo.Value, 1).AddMonths(1).AddDays(-1);
        }
        bool tag = true;
        private void dtFrom_ValueChanged(object sender, EventArgs e)
        {
            tag = false;
            this.cmbMonthFrom.Value = this.dtFrom.DateTime.Month;
            tag = true;
        }

        private void dtTo_ValueChanged(object sender, EventArgs e)
        {
            tag = false;
            this.cmbMonthTo.Value = this.dtTo.DateTime.Month;
            tag = true;
        }

        public DateTime DateFrom
        {
            get { return this.dtFrom.DateTime; }
            set { this.dtFrom.DateTime = value; }
        }
        public DateTime DateTo
        {
            get { return this.dtTo.DateTime; }
            set { this.dtTo.DateTime = value; }
        }

        private void grdStatements_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {

        }

        private void grdStatements_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e)
        {
            if (e.Row.Cells["HeaderTypeName"].Value.ToString().Trim() == "გადახდა")
            {
                e.Row.Appearance.ForeColor = Color.OrangeRed;
            }
        }


        protected void LoadReport()
        {
            this.reportViewer1.ProcessingMode = ProcessingMode.Remote;
            Report report = null;
            this.reportViewer1.ShowParameterPrompts = false;

            this.reportViewer1.ServerReport.ReportServerUrl = new Uri(Billing.SettingManager.getSetting("ReportService"));
            this.reportViewer1.ServerReport.ReportPath = "/BillingReports/PaymentAnalyses/StatementSelectByPeriod";
            this.reportViewer1.ServerReport.ReportServerCredentials.NetworkCredentials =
                new System.Net.NetworkCredential(
                    Billing.SettingManager.getSetting("ReportServiceUser"),
                    Billing.SettingManager.getSetting("ReportServicePassword"));

            report = this.reportViewer1.ServerReport;

  
            IList<ReportParameter> parameters = new List<ReportParameter>();

            parameters.Add(new ReportParameter("ClientID", this.ClientID.ToString()));
            parameters.Add(new ReportParameter("DateFrom", this.dtFrom.Value.ToString()));
            parameters.Add(new ReportParameter("DateTo", this.dtTo.Value.ToString()));

            if (parameters != null)
                report.SetParameters(parameters);


            reportViewer1.RefreshReport();
        }

    }
}
